package com.sunshine.simplehospital.ui.activity

import android.os.Bundle
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.snackbar.Snackbar
import com.sunshine.simplehospital.R
import com.sunshine.simplehospital.base.BaseActivity
import com.sunshine.simplehospital.bean.User
import com.sunshine.simplehospital.bean.UserInfo
import com.sunshine.simplehospital.ui.fragment.DefaultFragment
import com.sunshine.simplehospital.viewmodel.LoginViewModel
import kotlinx.android.synthetic.main.activity_user_register.*
import kotlinx.android.synthetic.main.fragment_user_register_end.*
import kotlinx.android.synthetic.main.fragment_user_register_first.*

class UserRegisterActivity : BaseActivity() {

    private lateinit var viewModel: LoginViewModel
    private lateinit var userInfo: UserInfo

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_register)
        viewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        userInfo = UserInfo()
        supportActionBar?.title = "注册简医"

        replaceFragment(R.layout.fragment_user_register_first)
        next_button.setOnClickListener {
            when{
                edit_user_id.text.isNullOrBlank() -> til_user_id.error = "请输入正确的账号"
                edit_password.text.isNullOrBlank() -> {
                    til_user_id.isErrorEnabled = false
                    til_password.error = "请输入密码"
                }
                edit_password.text.toString() != edit_password_again.text.toString() -> {

                    til_user_id.isErrorEnabled = false
                    til_password.isErrorEnabled = false
                    til_password_again.error = "两次输入的密码不一致"
                }
                else -> {
                    userInfo.userId = edit_user_id.text.toString()
                    userInfo.password = edit_password.text.toString()
                    replaceFragment(R.layout.fragment_user_register_end)
                }
            }

        }
    }

    private fun replaceFragment(@LayoutRes layoutResId: Int){
        val entering = layoutResId == R.layout.fragment_user_register_end
        val fragment = DefaultFragment().newInstance(layoutResId)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fragment_container, fragment as Fragment)
            .commit()
        if (entering) next_button.apply {
            text = "注册"
            setOnClickListener {
                when {
                    edit_pay_password.text.isNullOrBlank()||
                    edit_pay_password_again.text.isNullOrBlank()||
                    edit_user_name.text.isNullOrBlank()||
                    edit_age.text.isNullOrBlank()-> {
                        Snackbar.make(this, "请填写完整", Snackbar.LENGTH_SHORT).show()
                    }
                    edit_pay_password.text.toString() != edit_pay_password_again.text.toString() -> {
                        til_pay_password_again.error = "两次输入密码不一致"
                    }
                    else -> {
                        userInfo.apply {
                            payPassword = edit_pay_password.text.toString()
                            userName = edit_user_name.text.toString()
                            sex = if (radioButton_man.isChecked) 0 else 1
                            age = edit_age.text.toString().toInt()
                            address = edit_address.text.toString()
                            hasInsurance = radioButton_yes.isChecked
                            medicalHistory = edit_medical_history.text.toString()
                        }
                        viewModel.getRepository().insertUser(User(
                            userInfo.userId,
                            userInfo.password,
                            userInfo.payPassword,
                            userInfo.userName,
                            userInfo.hasInsurance,
                            userInfo.address,
                            userInfo.medicalHistory,
                            userInfo.sex,
                            userInfo.age,
                            userInfo.coin
                        ))
                        Toast.makeText(this@UserRegisterActivity, "注册成功", Toast.LENGTH_SHORT).show()
                        viewModel.setUserInfo(userInfo)
                        this@UserRegisterActivity.finish()
                    }
                }
            }
        }
    }
}